package com.example.toolproject.demo.controller;

import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.domain.UserEvaluation;
import com.example.toolproject.demo.service.UserEvaluationService;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName UserEvaluationController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-06-30 17:43
 **/
@Controller
@CrossOrigin
@RequestMapping("/userEvaluation")
public class UserEvaluationController {

    @Autowired
    private UserEvaluationService userEvaluationService;

    /**
     * 用户对撰写的报告进行评价
     * @param userEvaluation
     * @param request
     * @return
     */
    @PostMapping("/insertUserEvaluation")
    @ResponseBody
    public ResultBean insertUserEvaluation(@RequestBody UserEvaluation userEvaluation, HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        userEvaluation.setUserId(u.getId());
        try{
            boolean flag=userEvaluationService.insertUserEvaluation(userEvaluation);
            return ResultBean.success("用户对撰写的报告评价成功");
        }catch(Exception e){
            return ResultBean.error("用户对撰写的报告评价失败");
        }
    }
}
